/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.sapien.interceptor.InputErrorsAware;
import com.google.code.sapien.interceptor.UserAware;
import com.google.code.sapien.model.Category;
import com.google.code.sapien.model.Discussion;
import com.google.code.sapien.model.Response;
import com.google.code.sapien.model.User;
import com.google.code.sapien.service.CategoryService;
import com.google.code.sapien.service.ResponseService;
import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * Discussion Action, used to create, update, remove and view discussions.
 * @author Adam
 * @version $Id: ResponseAction.java 25 2009-05-26 04:23:01Z a.ruggles $
 * 
 * Created on Mar 7, 2009 at 10:44:07 AM 
 */
@Validations(
		requiredStrings = {
				@RequiredStringValidator(fieldName = "body",
						message = "Required Field.", key = "validation.required")
		} 
)
public class ResponseAction extends ActionSupport implements UserAware, ModelDriven<Response>, Preparable,
	InputErrorsAware {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = -3526899800953902026L;

	/**
	 * A List of categories for displaying in the view.
	 */
	private List<Category> categories;

	/**
	 * The category service.
	 */
	private final CategoryService categoryService;

	/**
	 * The discussion id.
	 */
	private Long discussionId;

	/**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private static final Logger LOG = LoggerFactory.getLogger(ResponseAction.class);

	/**
	 * The logged in user.
	 */
	private User currentUser;

	/**
	 * Response entity data model.
	 */
	private Response response;

	/**
	 * The response service.
	 */
	private final ResponseService responseService;

	/**
	 * Constructs a Response Action.
	 * @param responseService The response service.
	 * @param categoryService The category service.
	 */
	@Inject
	public ResponseAction(final ResponseService responseService, final CategoryService categoryService) {
		super();
		this.categoryService = categoryService;
		this.responseService = responseService;
	}

	/**
	 * Prepares the response add page.
	 * @return SUCCESS.
	 */
	@Action("new-response")
	@SkipValidation
	public String add() {
		categories = categoryService.getAll();
		return SUCCESS;
	}

	/**
	 * Creates a new response.
	 * @return SUCCESS.
	 */
	@Action(value = "create-response", results = {
			@Result(name = "input", location = "new-response.jsp"),
			@Result(name = "success", type = "redirectAction",
					params = {"id", "${discussionId}",
						"actionName", "discussion" })
			}
	)
	public String create() {
		response.getDiscussion().setId(discussionId);
		responseService.add(response, currentUser);
		addActionMessage(this.getText("create-response.success", "The response was created successfully."));
		return SUCCESS;
	}

	/**
	 * Returns categories.
	 * @return the categories.
	 */
	public List<Category> getCategories() {
		return categories;
	}
	/**
	 * Returns discussionId.
	 * @return the discussionId.
	 */
	public Long getDiscussionId() {
		return discussionId;
	}

	/**
	 * {@inheritDoc}
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public Response getModel() {
		return response;
	}

	/**
	 * {@inheritDoc}
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	public void prepare() {
		if (response == null) {
			response = new Response();
		}
		if (response.getDiscussion() == null) {
			response.setDiscussion(new Discussion());
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.interceptor.InputErrorsAware#prepareInputErrors()
	 */
	public void prepareInputErrors() {
		LOG.debug("Input error occurred preparing the view");
		categories = categoryService.getAll();
	}

	/**
	 * Sets discussionId.
	 * @param discussionId the discussionId to set.
	 */
	public void setDiscussionId(final Long discussionId) {
		this.discussionId = discussionId;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.interceptor.UserAware#setCurrentUser(User)
	 */
	public void setCurrentUser(final User currentUser) {
		LOG.debug("Setting the online user {}", currentUser);
		this.currentUser = currentUser;
	}
}
